package it.ricettario;

//ImagePanel
//
//Copyright (C) by Andrea Carboni.
//This file may be distributed under the terms of the LGPL license.
//

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Scrollable;
import javax.swing.SwingConstants;

public class ImagePanel extends JPanel implements Scrollable{
	public Dimension getPreferredScrollableViewportSize() {
		// TODO Auto-generated method stub
		return super.getPreferredSize();
	}

	public int getScrollableBlockIncrement(Rectangle visibleRect,
			int orientation, int direction) {
        if (orientation == SwingConstants.HORIZONTAL) {
            return visibleRect.width - 10;
        } else {
            return visibleRect.height - 10;
        }
	}

	public boolean getScrollableTracksViewportHeight() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean getScrollableTracksViewportWidth() {
		// TODO Auto-generated method stub
		return false;
	}

	public int getScrollableUnitIncrement(Rectangle visibleRect,
			int orientation, int direction) {
        //Get the current position.
        int currentPosition = 0;
        if (orientation == SwingConstants.HORIZONTAL) {
            currentPosition = visibleRect.x;
        } else {
            currentPosition = visibleRect.y;
        }

        //Return the number of pixels between currentPosition
        //and the nearest tick mark in the indicated direction.
        if (direction < 0) {
            int newPosition = currentPosition -
                             (currentPosition / 10)
                              * 10;
            return (newPosition == 0) ? 10 : newPosition;
        } else {
            return ((currentPosition / 10) + 1)
                   * 10
                   - currentPosition;
        }
	}

	private ImageIcon image = new ImageIcon();
	private int margin = 0;

	// ---------------------------------------------------------------------------

	public ImagePanel() {
	}

	// ---------------------------------------------------------------------------

	public ImagePanel(String imageFile) {
		setImage(imageFile);
	}

	// ---------------------------------------------------------------------------

	public void setImage(String imageFile) {
		image = new ImageIcon(imageFile);

		updatePrefSize();
	}

	// ---------------------------------------------------------------------------

	public void setMargin(int m) {
		margin = m;

		updatePrefSize();
	}

	// ---------------------------------------------------------------------------
	// ---
	// --- Internal methods
	// ---
	// ---------------------------------------------------------------------------

	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		if (image.getImage() != null)
			g.drawImage(image.getImage(), margin, margin, this);
	}

	// ---------------------------------------------------------------------------

	private void updatePrefSize() {
		int iw = image.getIconWidth();
		int ih = image.getIconHeight();

		setPreferredSize(new Dimension(iw + margin * 2, ih + margin * 2));
	}
	
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("ScrollPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new ImagePanel("C:\\Progetti\\Ricettario\\workspace\\Ricettario\\Ricette\\1 - ANTIPASTI\\focaccette con olive.jpg");
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }	
}
